local Piece = require "packages.chess-games.nchess.core.piece"

local Pawn = Piece:subclass("NChess.Pawn")

function Pawn:initialize(id)
  Piece.initialize(self, id)

  self.name = 'pawn'
  self.whiteStr = '兵'
  self.blackStr = '兵'
end

---@param board integer[][]
---@param fromX integer
---@param fromY integer
---@param x integer
---@param y integer
function Pawn:canMoveTo(board, fromX, fromY, x, y)
  local room = Fk:currentRoom() --[[@as NChess.RoomBase]]
  local dx = x - fromX
  local dy = y - fromY
  local myColor = self:getColor()
  local targetId = board[y][x]

  if dx == 0 and targetId == 0 then
    if myColor == "white" then
      if not room:beenMoved(self.id) and (dy == -1 or dy == -2) and board[fromY-1][fromX] == 0 then
        return true
      elseif dy == -1 then
        return true
      end
    else
      if not room:beenMoved(self.id) and (dy == 1 or dy == 2) and board[fromY+1][fromX] == 0 then
        return true
      elseif dy == 1 then
        return true
      end
    end
  elseif math.abs(dx*dy) == 1 and targetId ~= 0 then
    local targetColor = (targetId > 0) and "white" or "black"
    if myColor ~= targetColor then
      if myColor == "white" and dy == -1 then
        return true
      elseif myColor == "black" and dy == 1 then
        return true
      end
    end
  elseif math.abs(dx*dy) == 1 and board[fromY][x] ~= 0 then
    local targetColor = board[fromY][x] > 0 and "white" or "black"
    if myColor ~= targetColor and board[fromY][x] == room:getPassedPawn() then
      if myColor == "white" and dy == -1 then
        return true
      elseif myColor == "black" and dy == 1 then
        return true
      end
    end
  end

  return false
end

return Pawn

